﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;
using ICSharpCode.TextEditor;
using System.IO;

namespace UnrealDebugger.Panels
{
    public partial class OutputPanel : Panel
    {
        public OutputPanel()
            : base()
        {
            UnrealDebuggerIDE.Instance.OnProjectLoaded += new UnrealDebuggerIDE.ProjectLoadedDelegate(OnProjectLoaded);
            UnrealDebuggerIDE.Instance.OnEditorPropertiesChanged += new UnrealDebuggerIDE.EditorPropertiesChangedDelegate(OnEditorPropertiesChanged);

            InitializeComponent();
        }

        public void OnProjectLoaded(string filename)
        {
            foreach (Control control in this.tabControlOutput.Controls)
            {
                TabPage tabPage = control as TabPage;
                TextEditorControl textEditorControl = tabPage != null ? tabPage.Tag as TextEditorControl : null;
                if (textEditorControl != null) { textEditorControl.Font = UnrealDebuggerIDE.Instance.Project.EditorFont; }
            }
        }

        public void OnEditorPropertiesChanged()
        {
            foreach (Control control in this.tabControlOutput.Controls)
            {
                TabPage tabPage = control as TabPage;
                TextEditorControl textEditorControl = tabPage != null ? tabPage.Tag as TextEditorControl : null;
                if (textEditorControl != null) { textEditorControl.Font = UnrealDebuggerIDE.Instance.Project.EditorFont; }
            }
        }

        #region Debugger Interface

        public void AddLineToLog(string line)
        {
            string[] tokens = line.Split(new char[] { ' ', '\t', '\n' }, StringSplitOptions.RemoveEmptyEntries);                                   
            string category = tokens.Length > 0 && tokens[0].Contains(':') ? tokens[0].Trim() : "Output:";

            line = line.Replace(category, "");
            if (!line.EndsWith("\n")) { line += '\n'; }

            string text = DateTime.Now.ToString("[h:mm:ss]  ") + line;

            List<Control> outputPages = this.tabControlOutput.Controls.Find("tabPage" + category, false).ToList<Control>();

            if (outputPages.Count == 0)
            {
                TabPage tabPage = new TabPage();
                TextEditorControl textEditorControl = new TextEditorControl();

                tabPage.ContextMenuStrip = this.contextMenuStripLog;
                tabPage.SuspendLayout();

                this.tabControlOutput.Controls.Add(tabPage);

                textEditorControl.Dock = System.Windows.Forms.DockStyle.Fill;
                textEditorControl.IsReadOnly = true;
                textEditorControl.Name = "textEditorControl" + category;
                textEditorControl.Font = UnrealDebuggerIDE.Instance.Project.EditorFont;

                tabPage.Controls.Add(textEditorControl);
                tabPage.Name = "tabPage" + category;
                tabPage.Padding = new System.Windows.Forms.Padding(3);
                tabPage.Text = category;
                tabPage.UseVisualStyleBackColor = true;
                tabPage.Tag = textEditorControl;

                tabPage.ResumeLayout();
                outputPages.Add(tabPage);
            }

            foreach (Control control in outputPages)
            {
                TabPage tabPage = control as TabPage;
                TextEditorControl textEditorControl = tabPage != null ? tabPage.Tag as TextEditorControl : null;

                if (textEditorControl != null)
                {
                    textEditorControl.IsReadOnly = false;
                    textEditorControl.ActiveTextAreaControl.TextArea.InsertString(text);
                    textEditorControl.ActiveTextAreaControl.ScrollToCaret();
                    textEditorControl.IsReadOnly = true;
                }
            }
        }

        #endregion

        private void saveToFileToolStripMenuItem_Click(object sender, EventArgs e)
        {
            TabPage tabPage = this.tabControlOutput.SelectedTab;
            TextEditorControl textEditorControl = tabPage != null ? tabPage.Tag as TextEditorControl : null;

            if (textEditorControl != null)
            {
                Stream output = null;
                SaveFileDialog dlg = new SaveFileDialog();

                dlg.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
                dlg.FilterIndex = 1;
                dlg.RestoreDirectory = true;

                if (dlg.ShowDialog() == DialogResult.OK)
                {
                    if ((output = dlg.OpenFile()) != null)
                    {
                        StreamWriter writer = new StreamWriter(output);
                        writer.Write(textEditorControl.Document.TextContent);
                        writer.Close();
                        output.Close();
                    }
                }
            }
        }

        private void clearLogToolStripMenuItem_Click(object sender, EventArgs e)
        {
            TabPage tabPage = this.tabControlOutput.SelectedTab;
            TextEditorControl textEditorControl = tabPage != null ? tabPage.Tag as TextEditorControl : null;

            if (textEditorControl != null)
            {
                textEditorControl.IsReadOnly = false;
                textEditorControl.Document.TextContent = "";
                textEditorControl.IsReadOnly = true;
                textEditorControl.Refresh();
            }
        }
    }
}
